package export

import (
	"fmt"
	"game/game-design/loader"
	"strings"
)

type ExportArgs struct {
	SourceHome      string //资源目录
	LanguageFile    string //语言文件名
	LanguageClass   string //语言类名
	Mode            string //导出模式，fast快速导出，full完整导出
	ModeFile        string //用于快速导出时的文件MD5校验
	Exportfor       [][]string
	DesignDefine    *loader.DesignDefine
	Reader          IDesignIO
	Writer          IDesignIO
	DealFiles       map[string]int //处理哪些文件
	NeedFilterFiles bool           //是否要过滤文件
}

func NewExportArgs(args []string) *ExportArgs {
	var exportArgs ExportArgs
	exportArgs.DealFiles = make(map[string]int)
	for _, v := range args {
		parts := strings.Split(v, " ")
		if strings.HasPrefix(v, "-sourcehome") {
			exportArgs.SourceHome = parts[1]

		} else if strings.HasPrefix(v, "-language") {
			exportArgs.LanguageFile = parts[1]
			exportArgs.LanguageClass = parts[2]

		} else if strings.HasPrefix(v, "-exportfor") {
			values := make([]string, len(parts)-1)
			copy(values, parts[1:])
			exportArgs.Exportfor = append(exportArgs.Exportfor, values)

		} else if strings.HasPrefix(v, "-mode") {
			exportArgs.Mode = parts[1]
			exportArgs.ModeFile = parts[2]

		} else if strings.HasPrefix(v, "-define") {
			var designDefine loader.DesignDefine
			designDefine.Form(parts)
			exportArgs.DesignDefine = &designDefine

		} else if strings.HasPrefix(v, "-reader") {
			exportArgs.Reader = GetDesignIO(parts[1])

		} else if strings.HasPrefix(v, "-writer") {
			exportArgs.Writer = GetDesignIO(parts[1])

		} else if strings.HasPrefix(v, "-dealfiles") {
			for i, dealFile := range parts {
				if i == 0 {
					continue
				}
				if strings.TrimSpace(dealFile) == "" {
					continue
				}
				exportArgs.DealFiles[dealFile] = 1
				exportArgs.NeedFilterFiles = true
				fmt.Println("读取文件", dealFile)
			}
		}
	}
	return &exportArgs
}

func GetDesignIO(ioName string) IDesignIO {
	if ioName == "excel" {
		return &ExcelIO{}
	} else if ioName == "csv" {
		return &CsvIO{}
	} else if ioName == "tjson" {
		return &TextJsonIO{}
	}
	return nil
}

type IDesignIO interface {
	Read(fileFullName string, designDefine *loader.DesignDefine) ([]*loader.DesignFile, error)
	Write(designFile *loader.DesignFile, outputPath string, expectType string)
}
